package com.leza.myapplication.onenetapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonAnalysis {
    static private final HashMap<String,String> dataDict = new HashMap<>();

    static  {
        dataDict.put("temp", "temperature"); dataDict.put("Lightdata","Light_data"); dataDict.put("humi", "humidity"); dataDict.put("lll", "UVI");
        dataDict.put("rs485", "rs_light"); dataDict.put("ddd", "rain"); dataDict.put("eee","smoke"); dataDict.put("bbb", "soil_moisture");
        dataDict.put("ccc","flame"); dataDict.put("aaa", "soil_temperature");
    }

    static public List<EntityData> Analysis(String jsonString) {
        JSONObject jsonobj = JSON.parseObject(jsonString); //将json字符串转换成jsonObject对象
        System.out.println(jsonobj);
        JSONArray datastreams = jsonobj.getJSONObject("data").getJSONArray("datastreams");

        List<EntityData> res = new ArrayList<>();
        for (int i = 0; i < datastreams.size(); i++) {
            JSONArray t = datastreams.getJSONObject(i).getJSONArray("datapoints");
            EntityData entity = JSON.parseObject(String.valueOf(t.getJSONObject(t.size() - 1)), EntityData.class);

            entity.id = dataDict.get((String) datastreams.getJSONObject(i).get("id"));
//            System.out.println(entity);
            res.add(entity);
        }
        return res;
    }

}
